*Put directory path here, and put data here. Create a subfolder called "Output".
cd "Drive:\Folder"


*Cleaning GP Chars (birth yr, nationality; sex lower)
{
use aar BehandlerID  LegeAlder OpprLand using "FastLegeInfo.dta", clear
*Rename to English
rename 	(aar 	BehandlerID 	LegeAlder	OpprLand) ///
		(Year 	GPID 			Age 		BirthCountry)
*Drop if GP ID is missing
drop if GPID==.
*dropping GPs with multiple observations per Year
bysort GPID Year: gen n=_n
keep if n==1
drop n
*reshape wide (1 ob per GP and fill in based on all years)
reshape wide Age BirthCountry, i(GPID) j(Year)
gen YearBorn_GP = .
gen BirthCountry=""
forvalues yr = 2002/2014{
replace YearBorn_GP = `yr' - Age`yr' if YearBorn_GP==. & Age`yr'!=.
label var YearBorn_GP "Year Born GP"
replace BirthCountry = BirthCountry`yr' if BirthCountry`yr'!="" & BirthCountry==""
}
*Assign Norway born or not
gen Norway_GP = 1 if BirthCountry==""
replace Norway_GP = 0 if BirthCountry!=""
keep  YearBorn_GP Norway_GP GPID
save "GP_Chars.dta", replace //use for balance on GPID
*Create variables for previous GP
rename (YearBorn_GP Norway_GP GPID) ///
	(YearBorn_prevGP Norway_prevGP str_exogGPIDprev)
save "PrevGP_Chars.dta", replace //use for balance on GPID AND Main analysis with varying FEs
}

*Original variable definitions for below
/*
label var lopenr "Personal identifier"
label var lopenr_far "Father personal identifier"
label var lopenr_mor "Mother personal identifier"
label var NORborn "Child born in Norway"
label var NORborn_dad "Father born in Norway"
label var NORborn_mom "Mother born in Norway"
label var birthorder "Birth order"
label var oldersiblings "Older siblings (number)"
label var sex "Child's gender (1=male)"
label var siblings "Number of siblings"

label var BU_dad`yr' "`yr' Father highest education"
label var BU_mom`yr' "`yr' Mother highest education"
label var Byr "Birth year child"
label var Byr_birth1 "Birth year mother's first child"
label var Byr_dad "Father's birth year"
label var Byr_mom "Mother's birth year"
label var civ_mom`yr' "`yr' Raw mom marriage status"
	1 "Unmarried" 2 "Married" 3 "Widow/Widower" 4 "Divorced" 5 "Separated" ///
	6 "Registered partner" 7 "Separated partner" 8 "Divorced partner" 9 "Surviving partner"
	
label var bokomm`yr' "`yr' Municipality"

label var GPage_`yr' "`yr' GP Age"
label var GPID`yr' "`yr' GP identifier"
label var GPlistmax_`yr' "`yr' GP maximum list size"
label var GPsex_`yr' "`yr' GP Sex"
label var GPspec_`yr' "`yr' GP specialist indicator"
label var GPTotal`yr' "`yr' GP Total Visits"

label var m_swap`yr' "`yr' Patient moved municipality"
label var ma_swap`yr' "`yr' swap (or exit from list) becasue an individual has moved abroad."
label var o_swap`yr' "`yr' Ordinary, individual initiated swap."
label var other_swap`yr' "`yr' Other swap"
label var red_swap`yr' "`yr' GP reduced list size"
label var swap`yr' "`yr' Change GP"
label var t_swap`yr' "`yr'  GP terminated list"
*/


*GPsex - impute missing
{
use finalset, clear
keep GPsex_* GPID*
save temp_hold, replace
*Stack, rows are patient-level
forvalues yr = 2/15{
use temp_hold, clear
local year = 2000 + `yr'
keep GPsex_`year' GPID`year'
rename (GPsex_`year' GPID`year') (GPsex_ GPID)
gen Year = `year'
if `yr' > 2 append using temp_GPsex
save temp_GPsex, replace
}
erase temp_hold.dta
collapse GPsex_, by(Year GPID)
*Wide so 1 obs per GP
reshape wide GPsex_, i(GPID) j(Year)
*Loop through, gender equals later year gender if missing
forvalues year=2014(-1)2002{
local nextyr = `year' +1
replace GPsex_`year' = GPsex_`nextyr' if GPsex_`year'==.
}
save temp_GPsex, replace
*Long so merge back
forvalues yr = 2002/2015{
use temp_GPsex, clear 
keep GPID GPsex_`yr'
rename GPID GPID`yr'
save temp_GPsex`yr', replace
}
*Merge back to main data
forvalues yr = 2002/2014{
if `yr' == 2002 use finalset, clear
if `yr' > 2002 use finalset_GPsex, clear
drop GPsex_`yr' 
merge m:1 GPID`yr' using temp_GPsex`yr', gen(GPsex_merge_`yr')
*drop if GPsex_merge_`yr'==2
save finalset_GPsex, replace
erase temp_GPsex`yr'.dta
}
erase temp_GPsex.dta
}

*Load in data cleaned so far
use finalset_GPsex, clear
*Parents and patient at fixed time
{
******Total GP visits at age 15
*GPTotal* is missing for years with 0 visits
forvalues yr=2006/2014{
	replace GPTotal`yr' = 0 if GPTotal`yr' ==. & GPID`yr'!=.
}
gen GPvisit_age15 = .
forvalues yr = 2006/2014{
replace GPvisit_age15 = GPTotal`yr' if Byr == `yr' - 15
}
label var GPvisit_age15 "Total GP visits at age 15"

******Total str exog swaps
gen total_red_swap_age15 = 0
forvalues yr = 2002/2015{
replace total_red_swap_age15 = total_red_swap_age15 + 1 if red_swap`yr'==1 & (`yr' - Byr <= 15)
}
label var total_red_swap_age15 "Count; reduce list swap; by age 15"
gen total_term_swap_age15 = 0
forvalues yr = 2002/2015{
replace total_term_swap_age15 = total_term_swap_age15 + 1 if t_swap`yr'==1 & (`yr' - Byr <= 15)
}
label var total_term_swap_age15 "Count; terminate list swap; by age 15"
gen total_str_exog_swap_age15 = total_term_swap_age15 + total_red_swap_age15 ///
	if total_term_swap_age15!=. & total_red_swap_age15!=.
label var total_str_exog_swap_age15 "Count; str exog swap; by age 15"

******mom_age_birth (mom age at birth)
gen mom_age_birth = .
replace mom_age_birth = Byr - Byr_mom if Byr_mom!=. & Byr!=.
label var mom_age_birth "Mom age at birth"

******BU_dad - dad_edu_yrs (at age 0)
gen dad_edu_yrs=.
forvalues yr=1992/2014{
replace dad_edu_yrs = 0 if Byr == `yr' & BU_dad`yr' < 100000
replace dad_edu_yrs = 7 if Byr == `yr' & BU_dad`yr' >= 100000 & BU_dad`yr' < 200000
replace dad_edu_yrs = 10 if Byr == `yr' & BU_dad`yr' >= 200000 & BU_dad`yr' < 300000
replace dad_edu_yrs = 13 if Byr == `yr' & BU_dad`yr' >= 300000 & BU_dad`yr' < 400000
replace dad_edu_yrs = 14 if Byr == `yr' & BU_dad`yr' >= 400000 & BU_dad`yr' < 500000
replace dad_edu_yrs = 15 if Byr == `yr' & BU_dad`yr' >= 500000 & BU_dad`yr' < 600000
replace dad_edu_yrs = 16 if Byr == `yr' & BU_dad`yr' >= 600000 & BU_dad`yr' < 700000
replace dad_edu_yrs = 17.5 if Byr == `yr' & BU_dad`yr' >= 700000 & BU_dad`yr' < 800000
replace dad_edu_yrs = 21.5 if Byr == `yr' & BU_dad`yr' >= 800000 & BU_dad`yr' < 900000
}
label var dad_edu_yrs "Dad years of education at birth"

******BU_mom - mom_edu_yrs (at age 0)
gen mom_edu_yrs=.
forvalues yr=1992/2014{
replace mom_edu_yrs = 0 if Byr == `yr' & BU_mom`yr' < 100000
replace mom_edu_yrs = 7 if Byr == `yr' & BU_mom`yr' >= 100000 & BU_mom`yr' < 200000
replace mom_edu_yrs = 10 if Byr == `yr' & BU_mom`yr' >= 200000 & BU_mom`yr' < 300000
replace mom_edu_yrs = 13 if Byr == `yr' & BU_mom`yr' >= 300000 & BU_mom`yr' < 400000
replace mom_edu_yrs = 14 if Byr == `yr' & BU_mom`yr' >= 400000 & BU_mom`yr' < 500000
replace mom_edu_yrs = 15 if Byr == `yr' & BU_mom`yr' >= 500000 & BU_mom`yr' < 600000
replace mom_edu_yrs = 16 if Byr == `yr' & BU_mom`yr' >= 600000 & BU_mom`yr' < 700000
replace mom_edu_yrs = 17.5 if Byr == `yr' & BU_mom`yr' >= 700000 & BU_mom`yr' < 800000
replace mom_edu_yrs = 21.5 if Byr == `yr' & BU_mom`yr' >= 800000 & BU_mom`yr' < 900000
}
label var mom_edu_yrs "Mom years of education at birth"
******BU_mom - mom_edu_yrs (at age 15)
gen mom_edu_yrs_15=.
forvalues yr=1992/2014{
replace mom_edu_yrs_15 = 0 if Byr + 15 == `yr' & BU_mom`yr' < 100000
replace mom_edu_yrs_15 = 7 if Byr + 15 == `yr' & BU_mom`yr' >= 100000 & BU_mom`yr' < 200000
replace mom_edu_yrs_15 = 10 if Byr + 15 == `yr' & BU_mom`yr' >= 200000 & BU_mom`yr' < 300000
replace mom_edu_yrs_15 = 13 if Byr + 15 == `yr' & BU_mom`yr' >= 300000 & BU_mom`yr' < 400000
replace mom_edu_yrs_15 = 14 if Byr + 15 == `yr' & BU_mom`yr' >= 400000 & BU_mom`yr' < 500000
replace mom_edu_yrs_15 = 15 if Byr + 15 == `yr' & BU_mom`yr' >= 500000 & BU_mom`yr' < 600000
replace mom_edu_yrs_15 = 16 if Byr + 15 == `yr' & BU_mom`yr' >= 600000 & BU_mom`yr' < 700000
replace mom_edu_yrs_15 = 17.5 if Byr + 15 == `yr' & BU_mom`yr' >= 700000 & BU_mom`yr' < 800000
replace mom_edu_yrs_15 = 21.5 if Byr + 15 == `yr' & BU_mom`yr' >= 800000 & BU_mom`yr' < 900000
}

******civ_mom - Mom married (at age 0)
gen mom_married=.
forvalues yr=1975/2014{
replace mom_married = 1 if Byr == `yr' & civ_mom`yr' == 2
replace mom_married = 0 if Byr == `yr' & civ_mom`yr' != 2 & civ_mom`yr' != .
}
label var mom_married "Mom married at birth"

******m_wsaminnt_ - Mom total income (log) (at age 15)
gen mom_log_tot_inc_15=.
forvalues yr=1993/2014{
replace mom_log_tot_inc_15 = ln(m_wsaminnt_`yr') if Byr + 15 == `yr' 
}
label var mom_log_tot_inc_15 "Mom log total income at age 15"

******m_arbled_ and m_wyrkinnt_ - Mom out of labor force (at age 15)
gen mom_NoLFP_15=.
forvalues yr=1993/2014{
replace mom_NoLFP_15 = (m_wyrkinnt_`yr'==. & m_arbled_`yr'==.) ///
	if Byr + 15 == `yr'
}
label var mom_NoLFP_15 "Mom out of labor force at age 15"
}

*Strict Exogenous (reduce, terminate)
{
******Strict exogenous swap
forvalues yr=2002/2015{
gen str_exog_swap`yr' = 1 if red_swap`yr' == 1 | t_swap`yr' == 1
replace str_exog_swap`yr' = 0 if str_exog_swap`yr' == .
label var str_exog_swap`yr' "Strict exogenous swap (reduce list, terminate list) - `yr'"
}

******Year of first strict exogenous swaps
gen yr_str_exog_swap=.
forvalues yr = 2015(-1)2002{
replace yr_str_exog_swap = `yr' if str_exog_swap`yr' == 1
}
label var yr_str_exog_swap "Year of first strict exogenous swap"
******Age at first strict exogenous swap
gen str_exog_age = .
replace str_exog_age = yr_str_exog_swap - Byr if yr_str_exog_swap!=. & Byr!=.
label var str_exog_age "Age at first strict exogenous swap"

******Muni of first strict exogenous swaps
gen str_exog_muni = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace str_exog_muni = bokomm`nxtyr' if yr_str_exog_swap == `yr'
}
label var str_exog_muni "Municipality of first strict exogenous swap"

******GPsex of first strict exogenous swaps
gen str_exog_GPsex = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace str_exog_GPsex = GPsex_`nxtyr' if yr_str_exog_swap == `yr'
}
label var str_exog_GPsex "GP sex of first strict exogenous swap"
******GP sex MATCH of first strict exogenous swaps
gen str_GPsexMatch = 1 if (str_exog_GPsex==1 & sex==1) | (str_exog_GPsex==0 & sex==2)
replace str_GPsexMatch = 0 if (str_exog_GPsex==0 & sex==1) | (str_exog_GPsex==1 & sex==2)
label var str_GPsexMatch "GP-patient sex match; strict exogenous swaps"

******GPID of first exogenous swaps (cluster)
gen str_exogGPID = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace str_exogGPID = GPID`nxtyr' if yr_str_exog_swap == `yr'
}
format str_exogGPID %12.0g
label var str_exogGPID "GP ID of first strict exogenous swap"

******Still with first exog swap at age 15
gen GPID_age15 = .
forvalues yr = 2002/2015{
replace GPID_age15 = GPID`yr' if Byr == `yr' - 15
}
gen str_keepexogGP = (str_exogGPID == GPID_age15) if str_exogGPID != .
label var str_keepexogGP "Still with first str exog swap at age 15"

******Previous GPID of first exogenous swaps (FE)
gen str_exogGPIDprev = .
forvalues yr = 2002/2015{
replace str_exogGPIDprev = GPID`yr' if yr_str_exog_swap == `yr'
}
format str_exogGPIDprev %12.0g
label var str_exogGPIDprev "Previous GP ID of first strict exogenous swap"

******Previous GP sex of first strict exogenous swaps 
gen str_exogGPsexprev = .
forvalues yr = 2002/2015{
replace str_exogGPsexprev = GPsex_`yr' if yr_str_exog_swap == `yr'
}
label var str_exogGPsexprev "Previous GP sex of first strict exogenous swap"

******Exposure: 15 minus age of first strict exogenous swap
gen exposure_str_exog_swap = 15 - (yr_str_exog_swap - Byr) if yr_str_exog_swap!=.
replace exposure_str_exog_swap = . if exposure_str_exog_swap < 0
replace exposure_str_exog_swap = exposure_str_exog_swap*str_GPsexMatch
label var exposure_str_exog_swap "Exposure measure: 15 minus age of first strict exogenous swap"
}

*Strict Strict Exogenous (terminate list only)
{
******Terminate swap
forvalues yr=2002/2015{
gen term_exog_swap`yr' = 1 if t_swap`yr' == 1
replace term_exog_swap`yr' = 0 if term_exog_swap`yr' == .
label var term_exog_swap`yr' "Terminate - `yr'"
}
******Year of first terminate swaps
gen yr_term_exog_swap=.
forvalues yr = 2015(-1)2002{
replace yr_term_exog_swap = `yr' if term_exog_swap`yr' == 1
}
label var yr_term_exog_swap "Year of first terminate swap"

******Muni of first terminate swaps
gen term_exog_muni = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace term_exog_muni = bokomm`nxtyr' if yr_term_exog_swap == `yr'
}
label var term_exog_muni "Municipality of first terminate swap"

******GPsex of first terminate swaps
gen term_exog_GPsex = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace term_exog_GPsex = GPsex_`nxtyr' if yr_term_exog_swap == `yr'
}
label var term_exog_GPsex "GP sex of first terminate swap"
******GP sex MATCH of first terminate exogenous swaps
gen term_GPsexMatch = 1 if (term_exog_GPsex==1 & sex==1) | (term_exog_GPsex==0 & sex==2)
replace term_GPsexMatch = 0 if (term_exog_GPsex==0 & sex==1) | (term_exog_GPsex==1 & sex==2)
label var term_GPsexMatch "GP-patient sex match; terminate swaps"

******GPID of first terminate swaps (cluster)
gen term_exogGPID = .
forvalues yr = 2002/2014{
local nxtyr = `yr' + 1
replace term_exogGPID = GPID`nxtyr' if yr_term_exog_swap == `yr'
}
format term_exogGPID %12.0g
label var term_exogGPID "GP ID of first terminate swap"

******Previous GPID of first terminate swaps (FE)
gen term_exogGPIDprev = .
forvalues yr = 2002/2015{
replace term_exogGPIDprev = GPID`yr' if yr_term_exog_swap == `yr'
}
format term_exogGPIDprev %12.0g
label var term_exogGPIDprev "Previous GP ID of first terminate swap"


******Exposure: 15 minus age of first terminate swap
gen exposure_term_exog_swap = 15 - (yr_term_exog_swap - Byr) if yr_term_exog_swap!=.
replace exposure_term_exog_swap = . if exposure_term_exog_swap1 < 0
replace exposure_term_exog_swap = exposure_term_exog_swap1*term_GPsexMatch
label var exposure_term_exog_swap "Exposure measure: 15 minus age of first terminate swap"
}

drop if lopenr==""
erase finalset_GPsex.dta
save MainRHS, replace








